$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var warehouse = "";

    //点击返回按钮
    $(".head").on("click", ".goBack", function () {
        $.confirm("确定返回吗？", function () {
            localStorage.removeItem("work");
            window.location.href = "returnorderRope.html";
        }, function () {
            $("#goods_id").focus();
        });
    });

    //搜索框 获取焦点
    $("#goods_id").focus();

    //点击 显示 隐藏
    $(".head .icon-xianshi").on("click", function () {
        $(this).hide();
        $(".head .icon-yincang").show();
        $(".details").show();
        //$alert("显示成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });
    $(".head .icon-yincang").on("click", function () {
        $(this).hide();
        $(".head .icon-xianshi").show();
        $(".details").hide();
        //$alert("隐藏成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });

    var work = JSON.parse(localStorage.getItem("work"));
    $(".details span.good_mc").html(work.productName);
    $(".details span.good_jsgg").html(work.specification);
    $(".details span.good_jsck").html(work.houseName.trim());
    $(".details span.good_zl").html(work.expectReturnWeight);

    //根据扫描内容 得到 产品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            var good_zl = $(".details span.good_zl").html();
            var good_weight = $(".details span.good_weight").html();
            // if (Number(good_weight) > Number(accAdd(good_zl, 2000))) {
            //     $alert("扫描完成，请点击确认！");
            //     return;
            // }
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            //$("#goods_id").blur();
            if (!val) {
                $alert("请扫描退货产品标签！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/productsalereturn/getProductRopewarehouse',
                data: {
                    _id: work.id,
                    serial: val
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if ($(".content .list-block .goodsList_ul li[attr-data='" + (data.batch ? data.batch : data.number) + "']").length != 0) {
                            $alert("不可重复扫描");
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        } else {
                            $(".mengceng").show();
                            $("div.div_pt").show();
                            $("div.div_pt .cpph").val(data.batch);
                            $("div.div_pt .cpmz").val(data.grossQuantity).focus();
                            $("div.div_pt .cppz").val(data.tareQuantity);
                            warehouse = data;
                            //$("div.div_pt .location").val("");
                            // var data_html = '<li attr-data="' + data.batch + '"><div class="row no-gutter">' +
                            //     '<div class="col-90">' +
                            //     '<p><p><span>编号:</span><span class="sp goods_cpph">' +
                            //     data.batch + '</span></p><p><span>规格:</span><span class="sp goods_cpgg">' +
                            //     data.inventorySpecification + '</span></p>' +
                            //     '<p><span>炉号:</span><span class="sp goods_stove">' + data.stove + '</span>' +
                            //     '<span>卷号:</span><span class="sp goods_stove">' + data.column + '</span></p>' +
                            //     '<p><span>重量:</span><span class="sp goods_cpzl">' +
                            //     data.availableQuantity + '</span></p></div>' +
                            //     '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                            // $(".content .list-block .goodsList_ul").prepend(data_html);
                            // $alert("获取成功", 800);
                            // badge_text();
                            // setTimeout(function () {
                            //     $("#goods_id").val("").focus();
                            // }, 1400);
                        }
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    $("div .pt_quxiao").on("click", function () {
        $(".mengceng").hide();
        $("div.div_pt").hide();
        $("#goods_id").val("").focus();
    });

    //库位扫描
    $("div.div_pt input.location").on("keydown", function (event) {
        if (event.keyCode == "13") {
            var val = $(this).val();
            $("div.div_pt input.location").val("");
            if (!val) {
                $alert("请扫描库位！");
                setTimeout(function () {
                    $("div.div_pt input.location").val("").focus();
                }, 1400);
                return;
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/productsalereturn/getRopeLocationByHouse',
                    data: {
                        warehouseCode: work.houseCode,
                        location: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        loading_hide();
                        if (res.success) {
                            $("div.div_pt .location").val(res.result.location);
                        } else {
                            $("div.div_pt .location").val("");
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    $("div .pt_queding").on("click", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        var cpph = $("div.div_pt .cpph").val();
        var cpmz = $("div.div_pt .cpmz").val();
        var cppz = $("div.div_pt .cppz").val();
        var location = $("div.div_pt .location").val();
        if (!cpmz) {
            pass = 1;
            return $alert("请输入毛重！");
        }
        if (Number(cpmz) <= 0) {
            pass = 1;
            return $alert("毛重应大于0！");
        }
        if (!cppz) {
            pass = 1;
            return $alert("请输入皮重！");
        }
        if (Number(cppz) < 0) {
            pass = 1;
            return $alert("皮重应大于0！");
        }
        if (Number(cpmz) <= Number(cppz)) {
            pass = 1;
            return $alert("毛重应大于皮重！");
        }
        if (!location) {
            pass = 1;
            return $alert("请扫描库位！");
        }
        loading_show("确认中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/productsalereturn/getLocationByHouse',
            data: {
                warehouseCode: work.houseCode,
                location: location
            },
            dataType: 'json',
            json: 'callback',
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    var data_html = '<li attr-data="' + cpph + '"><div class="row no-gutter">' +
                        '<div class="col-90">' +
                        '<p><p><span>编号:</span><span class="sp goods_cpph">' +
                        cpph + '</span></p><p><span>规格:</span><span class="sp goods_cpgg">' +
                        work.specification + '</span></p>' +
                        '<p><span>毛重:</span><span class="sp goods_cpmz">' +
                        cpmz + '</span><span>皮重:</span><span class="sp goods_cppz">' +
                        cppz + '</span></p><p><span>库位:</span><span class="sp goods_kw">' +
                        location + '</span></p></div>' +
                        '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span>';
                    if (cpph.indexOf("CPTH") > -1) {
                        data_html = data_html + '<span class="icon iconfont icon-dayin pull-right"></span>'
                    }
                    data_html += '</div></div></li>';
                    $(".content .list-block .goodsList_ul").prepend(data_html);
                    $alert("添加成功", 800);
                    $(".mengceng").hide();
                    $("div.div_pt").hide();
                    badge_text();
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    if (cpph.indexOf("CPTH") > -1) {
                        cordova.plugins.print.wirerodpurchaseinorder(cpph, "", work.specification, accSub(cpmz, cppz), function (result) {
                        }, function error(error) {
                        });
                    }
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                loading_hide();
                console.log(error);
                $alert(error.responseText);
            }
        });
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10 .icon-msnui-jian", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    // li 中的 打印
    $(".content").on("click", ".goodsList_ul li .col-10 .icon-dayin", function () {
        var jianhao = $(this).parent().parent().parent();
        var number = jianhao.find("span.goods_cpph").text();
        var weight = jianhao.find("span.goods_cpmz").text();
        var pz = jianhao.find("span.goods_cppz").text();
        var workplanNumber = jianhao.find("span.goods_pcd").text();
        $alert(number + "打印成功");
        cordova.plugins.print.wirerodpurchaseinorder(number, "", work.specification, accSub(weight, pz), function (result) {
        }, function error(error) {
        });
    });

    //点击 获取编号
    $(".foot").on("click", ".add_go", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/productsalereturn/getReturnNumber',
            data: {},
            dataType: 'json',
            json: 'callback',
            cache: false,
            success: function (res) {
                pass = 1;
                if (res.success) {
                    var data = res.result;
                    $(".mengceng").show();
                    $("div.div_pt").show();
                    $("div.div_pt input.cpph").val(data.number ? data.number : "");
                    $("div.div_pt .cpmz").val("").focus();
                    $("div.div_pt .cppz").val("");
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                console.log(error);
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                $alert(error.responseText);
            }
        });
    });

    // 点击 添加出库 信息 传给后台
    $(document).on("click", ".foot .add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请添加退货的产品！");
        }
        $.confirm('共' + $(".content .goodsList_ul li").length + '件，确定提交？', function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            var datas = [];
            for (var i = 0; i < $(".content .goodsList_ul li").length; i++) {
                datas.push({
                    batch: $(".content .goodsList_ul li").eq(i).find("span.goods_cpph").text(),
                    grossQuantity: $(".content .goodsList_ul li").eq(i).find("span.goods_cpmz").text(),
                    tareQuantity: $(".content .goodsList_ul li").eq(i).find("span.goods_cppz").text(),
                    location: $(".content .goodsList_ul li").eq(i).find("span.goods_kw").text()
                });
            }
            loading_show("提交中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/productsalereturn/saveProductRopesalereturnorder",
                data: {
                    _id: work.id,
                    datas: JSON.stringify(datas)
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        loading_hide();
                        console.log("成品销售退货成功");
                        localStorage.removeItem("work");
                        window.location.href = "storekeeperRope.html";
                    } else {
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error.status == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        })
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        var allTareWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpmz").html();
            var tare_weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cppz").html();
            allWeight = accAdd(allWeight, weight);
            allTareWeight = accAdd(allTareWeight, tare_weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
        $(".details span.good_Pweight").html(tare_weight);
    }

    badge_text();

});